package com.anytron.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.quartz.CronExpression;
import org.quartz.TriggerUtils;
import org.quartz.impl.triggers.CronTriggerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anytron.model.AppTimer;
import com.anytron.model.req.PageRequest;
import com.anytron.model.res.ResultData;
import com.anytron.service.AppInfoService;
import com.anytron.service.AppTimerService;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping(value = "app/timer")
public class AppTimerController {
	
	@Autowired
	AppTimerService service;
	@Autowired
	AppInfoService appInfoService;
	
	@RequestMapping(value = "list")
	public ResultData<PageInfo<AppTimer>> list(PageRequest pageRequest,AppTimer appTimer) throws Exception {
		ResultData<PageInfo<AppTimer>> resultData = new ResultData<>();
		PageInfo<AppTimer> pageInfo = service.queryDataByPage(pageRequest, appTimer);
		resultData.setData(pageInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "execTimes" ,method = RequestMethod.POST)
	public ResultData<List<String>> execTimes(@RequestBody AppTimer appTimer) throws Exception {
		ResultData<List<String>> resultData = new ResultData<>();
		AppTimer one = service.getByKey(appTimer);
		String timerCron = one.getTimerCron();
		
		List<String> list = new ArrayList<>();
		CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
		cronTriggerImpl.setCronExpression(timerCron);
		List<Date> dates =TriggerUtils.computeFireTimes(cronTriggerImpl, null, 10);
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		for (Date date : dates) {
			list.add(dateFormat.format(date)); 
		}
		resultData.setData(list);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "add" ,method = RequestMethod.POST)
	public ResultData<String> add(@RequestBody AppTimer appTimer) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		boolean isValidExpression = CronExpression.isValidExpression(appTimer.getTimerCron());
		if (!isValidExpression) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("你输入的表达式不合规");
			resultData.setSuccess(false);
			return resultData;
		}
		AppTimer select = new AppTimer();
		select.setAppId(appTimer.getAppId());
		AppTimer one = service.getOne(select);
		if (one!=null) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("当前应用已配置过定时任务,请重新选择");
			resultData.setSuccess(false);
			return resultData;
		}
		if (appTimer.getIsStop()==null) {
			appTimer.setIsStop(0);
		}
		int res = service.add(appTimer);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("保存失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "update")
	public ResultData<String> update(@RequestBody AppTimer appTimer) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		boolean isValidExpression = CronExpression.isValidExpression(appTimer.getTimerCron());
		if (!isValidExpression) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("你输入的表达式不合规");
			resultData.setSuccess(false);
			return resultData;
		}
		AppTimer select = new AppTimer();
		select.setAppId(appTimer.getAppId());
		AppTimer one = service.getOne(select);
		if (one!=null && one.getId().longValue()!=appTimer.getId().longValue()) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("所选应用已配置过定时任务,请重新选择");
			resultData.setSuccess(false);
			return resultData;
		}
		if (appTimer.getIsStop()==null) {
			appTimer.setIsStop(0);
		}
		int res = service.update(appTimer);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "delete")
	public ResultData<String> delete(@RequestBody AppTimer appTimer) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = service.delete(appTimer.getId());
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("删除失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
}
